﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Edge.Business.Security;
using Edge.Common.ControlFlow;
using Edge.Common.Services;
using Shop.Business.Domain.Catalog;
using Shop.Business.Domain.Customers;
using Shop.Business.Domain.Pricing;

namespace Shop.Business.Domain.Orders.Logic
{
    public class AddProduct : BaseOrderOperation
    {
        private Product product;
        private int orderItemId;
        private int quantity;
        private bool update = false;
        private bool addSize = false;
        private bool convertTo = false;

        public bool ConvertTo
        {
            get { return convertTo; }
            set { convertTo = value; }
        }

        public Product Product
        {
            get { return product; }
            set { product = value; }
        }

        public int Quantity
        {
            get { return quantity; }
            set { quantity = value; }
        }


        public bool Update
        {
            get { return update; }
            set { update = value; }
        }


        public int OrderItemId
        {
            get { return orderItemId; }
            set { orderItemId = value; }
        }

        public bool AddSize
        {
            get { return addSize; }
            set { addSize = value; }
        }

        public override bool IsAllowed()
        {
            bool baseRet = base.IsAllowed();
            if (!baseRet)
                return false;

                //int qty = Locator.GetService<IProductQuantityService>().GetQuantity(product);
                // TODO change type of product.State to enum !!!
                if (!product.Visible)
                    return false;
                /*
                                if (qty == 0)
                                    return false;
                */
                //States parsedState = (States)Enum.Parse(typeof(States), product.State);
                if (order.OrderStatus == OrderStatus.Error)
                    return false;
                /*

                                decimal price =
                                    Locator.GetService<IProductPriceService>().GetPrice(product,
                                                                                        (order != null && order.Customer != null
                                                                                             ? order.Customer.Id
                                                                                             : 0));
                                if (price <= 0)
                                    return false;
                */


            return true;
        }

        public override void Execute(OrderController.OperationContext context)
        {
            //Assert.Check(quantity > 0);

            if (Update)
            {
                OrderItem existsItem = null;
                if (OrderItemId > -1)
                    existsItem = Locator.GetService<IOrderItemManager>().Load(OrderItemId);
                else
                {
                    foreach (OrderItem orderItem in order.Items)
                    {
                        if (orderItem.Product.Id == product.Id)
                        {
                            existsItem = orderItem;
                            break;
                        }
                    }
                }

                if (existsItem != null)
                {
                    existsItem.Quantity += quantity;
                    existsItem.AddSize = addSize;
                    if (convertTo)
                        existsItem.PriceItem.Qty -= quantity;
                    //Locator.GetService<IOrderItemManager>().Save(existsItem);
                }
                else
                {
                    AddProductToOrder(order, product, quantity, addSize, convertTo);
                }
            }
            else
            {
                AddProductToOrder(order, product, quantity, addSize, convertTo);
            }

            //Update time & person in order
            OrderObject.ModifiedBy = Locator.GetService<IPersonManager<Customer>>().GetCurrentAuthor();
            OrderObject.ModifyDate = DateTime.Now;
        }

//        public static ProductItem AddProductToOrder(Order order, int productId, int quantity, string endUser, string costCentre)
//        {
//            return AddProductToOrder(order, productId, quantity, endUser, costCentre, null, null, null, null);
//        }

        public static OrderItem AddProductToOrder(Order order, Product product, int quantity, bool addSize, bool convertTo)
        {
            OrderItem newItem = new OrderItem();
            newItem.Quantity = quantity;
            newItem.AddSize = addSize;
            newItem.PriceItem = product.PriceItem;
            newItem.Price = product.Price;
            newItem.SellPrice = product.Price - (product.Price * ((Customer)order.CreatedBy).Discount / 100);
            newItem.Product = product;
            newItem.Discount = ((Customer)order.CreatedBy).Discount;
            newItem.CreatedBy = order.CreatedBy;
            newItem.CreateDate = DateTime.Now;
            if (convertTo)
                product.PriceItem.Qty -= quantity;
            newItem.Order = order;
            order.Items.Add(newItem);
            //Locator.GetService<IOrderItemManager>().Save(newItem);
            return newItem;
        }

        #region IOperation Members

        public override IRestriction GetRestriction()
        {
            return null;
        }

        #endregion
    }
}